/* 
 * $Id$
 * CONFIDENTIAL AND PROPRIETARY. (c) 2007 Revolution Health Group LLC. All rights reserved. 
 * This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
 * 
 */
package org.javason.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ObjectUtils {
	public static final String VERSION = "$Rev: 63219 $";
	
	private static final Set<Class> SIMPLE_TYPES = new HashSet<Class>() {{
		add(Boolean.class); add(boolean.class);
		add(Float.class); add(float.class);
		add(Double.class); add(double.class);
		add(Integer.class); add(int.class);
		add(Long.class); add(long.class);
		add(Short.class); add(short.class);
		add(Byte.class); add(byte.class);
		add(String.class);
		add(BigDecimal.class);
		add(BigInteger.class);
	}};
	

	private ObjectUtils() {}
	
	
	
	public static boolean isMap(Object obj) {
		if (obj instanceof Map) {
			return true;
		}
		return false;
	}


	public static boolean isArray(Object obj) {
		if (obj != null) {
            return isClassArray(obj.getClass());
        }
		return false;
	}
	
    public static boolean isClassArray(Class clazz) {
        if (clazz.isArray()) {
            return true;
        }
        if (Collection.class.isAssignableFrom(clazz)) {
            return true;
        }
        return false;
    }
    
	public static boolean isSimpleType(Object obj) {
		boolean result = false;
		if (obj != null) {
            result = isClassSimpleType(obj.getClass());
        }

		return result;
	}
	
    public static boolean isClassSimpleType(Class clazz) {
        boolean result = false;
        if (clazz != null && SIMPLE_TYPES.contains(clazz)) {
            result = true;
        }
        return result;
    }
    

	public static boolean isComplex(Object value) {
		Class type = value.getClass();
		return !(isSimpleType(value) || type.isEnum());
	}
	
	public static String getXmlEnumValue(Object enumObj) {
		String result = null;
		try {
			if (ClassUtils.isXmlEnum(enumObj.getClass())) {
				// XmlEnum will have a "fromValue" method to convert the const to the enum const 
				Method value = enumObj.getClass().getMethod("value");
				result = value.invoke(enumObj).toString();
			}
		} catch (Exception e) {
			// could happen..just give up and return null
		}
		return result;
	}
}
